<template>
	<view class="jztit">
		<view class="titl">
			<text>祭祖商品</text>
			<view class=""></view>
		</view>
		<view class="loc">
			{{ tableInfo.ancestralName }}{{ tableInfo.tanghaoName ? '-' + tableInfo.tanghaoName : '' }}{{ tableInfo.tabletName ? '-' + tableInfo.tabletName : '' }}
		</view>
	</view>
	<view class="contentbox">
		<scroll-view scroll-y="true" class="scrleft">
			<view @click="setcate(item.id)" :class="item.id == cateId ? 'active' : ''" v-for="item in cateList" :key="item.id">{{ item.name }}</view>
		</scroll-view>
		<scroll-view scroll-y="true" class="scrright">
			<view class="tit">{{ cateName }}</view>
			<view class="rightitem" v-for="item in goodsList" :key="item.id">
				<image :src="item.imgs.split(',')[0]" mode="aspectFill" lazy-load></image>
				<view class="ritem">
					<text>{{ item.name }}</text>
					<view class="ritemb">
						<text>
							￥
							<text>{{ item.price }}</text>
						</text>
						<view class="rl" @click="chooserule(item)">选规格</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
	<view class="paybottom">
		<view class="payleft" @click="() => (cartShow = !cartShow)">
			<view class="cartpic">
				<image src="@/static/bag.png" mode=""></image>
				<view class="flex-center" v-if="cartNum > 0">{{ cartNum }}</view>
			</view>

			<text>
				￥
				<text>{{ cartMoney }}</text>
			</text>
		</view>
		<view class="payright flex-center" @click="pay">支付</view>
	</view>

	<up-popup :show="ruleShow" round="20rpx" mode="bottom" @close="ruleClose" :closeable="true">
		<scroll-view class="listbg">
			<view class="rightitem">
				<image :src="chooseInfo.imgs.split(',')[0]" mode=""></image>
				<view class="ritem">
					<view class="">
						<text class="aa">{{ chooseInfo.name }}</text>
						<view class="gys">已选：{{ chooseInfo.spName }}</view>
					</view>
					<view class="ritemb">
						<text>
							￥
							<text>{{ chooseInfo.price }}</text>
						</text>
					</view>
				</view>
			</view>
			<view class="rulename">规格</view>
			<view class="rulesbox">
				<view @click="setrules(item)" :class="chooseInfo.spName == item ? 'active flex-center' : 'flex-center'" v-for="(item, index) in chooseInfo.spNameList" :key="index">
					{{ item }}
				</view>
			</view>
			<view class="rulename" style="margin-top: 30rpx">
				<text>数量</text>
				<up-number-box :disabledInput="true" v-model="chooseInfo.goodsNum"></up-number-box>
			</view>
			<view class="addcar flex-center" @click="addcart">加入购物车</view>
		</scroll-view>
	</up-popup>

	<up-popup :show="cartShow" overlayStyle="z-index:55" zIndex="66" round="20rpx" mode="bottom" @close="cartClose" :closeable="true">
		<scroll-view :scroll-y="true" class="listbg">
			<view class="mytitle">
				<text>已选商品</text>
				<view class="" @click="clear">
					<image src="../../static/del.png" mode=""></image>
					<text>清空</text>
				</view>
			</view>

			<view class="rightitem rightitem2" v-for="(item, index) in cartList" :key="index">
				<image :src="item.imgs.split(',')[0]" mode="aspectFill"></image>
				<view class="ritem">
					<view class="">
						<text class="aa">{{ item.name }}</text>
						<view class="gys">已选：{{ item.spName }}</view>
					</view>
					<view class="ritemb">
						<text>
							￥
							<text>{{ item.price }}</text>
						</text>
						<up-number-box :min="0" :disabledInput="true" v-model="item.goodsNum" @change="valChange($event, index)"></up-number-box>
					</view>
				</view>
			</view>

			<view class="" style="height: 166rpx"></view>
		</scroll-view>
	</up-popup>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { ancestralgoodsList, addOrder, typeList } from '@/api/ancestra.js';
import { onLoad } from '@dcloudio/uni-app';
import { payment } from '@/utils/payment';

let tableInfo = ref(null);

let cateName = computed(() => {
	let name = '';
	cateList.value.forEach((item) => {
		if (item.id == cateId.value) {
			name = item.name;
		}
	});
	return name;
});

function valChange(e, index) {
	if (e.value == 0) {
		cartList.value.splice(index, 1);
		if (cartList.value.length == 0) {
			cartShow.value = false;
		}
	}
}

let cartNum = computed(() => {
	let num = 0;
	cartList.value.forEach((item) => {
		num = num + item.goodsNum * 1;
	});
	return num;
});

let cartMoney = computed(() => {
	let num = 0;
	cartList.value.forEach((item) => {
		num = num + item.goodsNum * 1 * item.price * 1;
	});
	return num;
});

function getCateName() {}
let cateList = ref([]);
let cateId = ref(0);

function getTypeList() {
	typeList({
		type: 1,
		pageNum: 1,
		pageSize: 999
	}).then((res) => {
		cateList.value = res.data;
		if (res.data.length) {
			cateId.value = res.data[0].id;
			getGoodsList();
		}
	});
}

function setcate(e) {
	cateId.value = e;
	getGoodsList();
}
let goodsList = ref([]);

function getGoodsList() {
	ancestralgoodsList({
		typeId: cateId.value,
		pageNum: 1,
		pageSize: 999
	}).then((res) => {
		goodsList.value = res.rows;
	});
}
let ruleShow = ref(false);
let num = ref(1);

function ruleClose() {
	ruleShow.value = false;
}

let cartShow = ref(false);

function cartClose() {
	cartShow.value = false;
}

function clear() {
	uni.showModal({
		title: '是否清空购物车',
		success: (res) => {
			if (res.confirm) {
				cartList.value = [];
				ruleShow.value = false;
			}
		}
	});
}
let chooseInfo = ref(null);

function chooserule(e) {
	let data = JSON.parse(JSON.stringify(e));
	data.goodsNum = 1;
	data.spNameList = JSON.parse(JSON.stringify(data.spName.split(',')));
	data.spName = '';
	chooseInfo.value = data;
	ruleShow.value = true;
}

function setrules(item) {
	chooseInfo.value.spName = item;
}
let cartList = ref([]);

function addcart() {
	if (!chooseInfo.value.spName) {
		uni.showToast({
			title: '请选择规格',
			icon: 'none'
		});
		return;
	}

	let info = JSON.parse(JSON.stringify(chooseInfo.value));
	let needAdd = true;
	cartList.value.forEach((item) => {
		if (item.id == info.id && item.spName == info.spName) {
			item.goodsNum = item.goodsNum * 1 + info.goodsNum * 1;
			needAdd = false;
		}
	});
	needAdd && cartList.value.push(info);
	ruleShow.value = false;
}

function pay() {
	if (!uni.getStorageSync('token')) {
		uni.showToast({
			title: '请先登录',
			icon: 'none'
		});
		setTimeout(() => {
			uni.navigateTo({
				url: '/pages/login/login'
			});
		}, 1500);
		return;
	} else {
		if (!uni.getStorageSync('openId')) {
			getWeChatCode();
		}
		let list = [];
		cartList.value.forEach((item) => {
			list.push({
				goodsId: item.id,
				goodsName: item.name,
				goodsNum: item.goodsNum,
				goodsPrice: item.price,
				goodsUrl: item.imgs,
				spName: item.spName
			});
		});
		let data = {
			tel: uni.getStorageSync('userInfo').phonenumber,
			price: cartMoney.value,
			ancestralId: tableInfo.value.ancestralId || '',
			ancestralName: tableInfo.value.ancestralName || '',
			tabletId: tableInfo.value.tabletId || '',
			tabletName: tableInfo.value.tabletName || '',
			payType: 0,
			orderGoodsList: list,
			modelType: 1,
			openId: uni.getStorageSync('openId')
		};
		if (!list.length) {
			uni.showToast({
				title: '请选择商品',
				icon: 'none'
			});
			return;
		} else {
			addOrder(data).then((result) => {
				let res = JSON.parse(result.data.payInfo);
				payment(res);
			});
		}
	}
}

const isWeixin = ref(false);
let wxCode = ref('');
onLoad((e) => {
	tableInfo.value = JSON.parse(e.data);
	getTypeList();

	isWeixin.value = isWechat();
	if (!uni.getStorageSync('openId')) {
		if (isWeixin.value) {
			checkWeChatCode(); //通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
		}
	}
});

// 授权
/*微信登录相关  start*/
//方法：用来判断是否是微信内置的浏览器
const isWechat = () => {
	return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === 'micromessenger';
};
//方法:用来提取code
const getUrlCode = (name) => {
	return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null;
};
//检查浏览器地址栏中微信接口返回的code
const checkWeChatCode = async () => {
	let code = getUrlCode('code');
	wxCode.value = code;
	if (code) {
		const res = await getOpenId({ wxCode: code });
		if (res.code == 200) {
			uni.setStorageSync('openId', res.data.openid);
		}
	}
};
//请求微信接口，用来获取code
const getWeChatCode = () => {
	let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址
	let appid = 'wx8063fe9f7ea16676';

	//通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
	window.location.href =
		'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + local + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
};
</script>

<style lang="scss" scoped>
.addcar {
	width: 690rpx;
	height: 80rpx;
	background: #c7a46f;
	border-radius: 40rpx;
	font-weight: bold;
	color: #ffffff;
	margin-top: 80rpx;
	margin-bottom: 30rpx;
}

.listbg {
	height: auto;
	width: 100%;
	box-sizing: border-box;
	padding: 24rpx;
	max-height: 1200rpx;

	.mytitle {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 600rpx;
		margin-bottom: 30rpx;

		& > text {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 28rpx;
			color: #111111;
		}

		view {
			display: flex;
			align-items: center;

			image {
				width: 30rpx;
				height: 30rpx;
				margin-right: 10rpx;
			}

			text {
				font-family: PingFang-SC, PingFang-SC;
				font-weight: 500;
				font-size: 22rpx;
				color: #999999;
			}
		}
	}
}

.rulesbox {
	display: flex;
	flex-wrap: wrap;

	view {
		width: auto;
		background: #f5f9fc;
		height: 80rpx;
		margin-right: 24rpx;
		margin-top: 30rpx;
		padding: 0 56rpx;
		border-radius: 12rpx;
	}

	.active {
		background: #ede7db;
		border: 1rpx solid #c7a46f;
		color: #c7a46f;
	}
}

.rulename {
	font-family: PingFang-SC, PingFang-SC;
	font-weight: bold;
	font-size: 30rpx;
	color: #111111;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-sizing: border-box;
	padding-right: 18rpx;
}

.rightitem {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;

	& > image {
		width: 190rpx;
		height: 190rpx;

		border-radius: 16rpx;
	}

	.ritem {
		display: flex;
		height: 190rpx;
		flex-direction: column;
		justify-content: space-between;
		flex: 1;
		margin-left: 16rpx;

		.gys {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #999999;
			line-height: 48rpx;
		}

		.aa {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #111111;
			width: 460rpx;
			display: block;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			/* 定义显示的最大行数 */
			-webkit-box-orient: vertical;
			overflow: hidden;
			text-overflow: ellipsis;
		}

		.ritemb {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.rl {
				width: 120rpx;
				height: 44rpx;
				background: #c7a46f;
				border-radius: 22rpx 22rpx 22rpx 22rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-family: PingFang-SC, PingFang-SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #ffffff;
			}

			text {
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 20rpx;
				color: #a9070c;

				text {
					font-size: 36rpx;
				}
			}
		}
	}
}

.rightitem2 {
	& > image {
		width: 130rpx;
		height: 130rpx;

		border-radius: 16rpx;
	}

	.ritem {
		display: flex;
		height: 140rpx;

		.gys {
			font-size: 22rpx;
			line-height: 36rpx;
		}

		.aa {
			font-size: 26rpx;
			color: #111111;
			width: 420rpx;
		}

		.ritemb {
			.rl {
				width: 120rpx;
				height: 44rpx;
				font-size: 20rpx;
			}

			text {
				font-size: 18rpx;

				text {
					font-size: 32rpx;
				}
			}
		}
	}
}

.contentbox {
	width: 100%;
	height: calc(100vh - var(--window-top) - 266rpx);
	box-sizing: border-box;
	display: flex;

	.scrleft {
		width: 152rpx;
		height: 100%;

		view {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #444444;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.active {
			font-weight: bold;
			font-size: 24rpx;
			color: #111111;
			background: #fff;
		}
	}

	.scrright {
		width: 598rpx;
		height: 100%;
		background: #fff;
		box-sizing: border-box;
		padding: 0 24rpx;

		.tit {
			height: 88rpx;
			line-height: 88rpx;
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 28rpx;
			color: #111111;
		}
	}
}

.paybottom {
	width: 100%;
	height: 166rpx;
	background: #fff;
	position: fixed;
	bottom: 0;
	left: 0;
	display: flex;
	z-index: 99;
	box-shadow: 5rpx 0rpx 15rpx 0rpx rgba(0, 0, 0, 0.2);
	justify-content: space-between;
	box-sizing: border-box;
	padding: 20rpx 32rpx 0 32rpx;

	.payright {
		width: 200rpx;
		height: 80rpx;
		background: #c7a46f;
		border-radius: 40rpx;
		font-family: PingFang-SC, PingFang-SC;
		font-weight: 500;
		font-size: 32rpx;
		color: #ffffff;
	}

	.payleft {
		display: flex;
		align-items: center;
		height: 82rpx;

		.cartpic {
			width: 82rpx;
			height: 82rpx;
			margin-right: 10rpx;
			position: relative;

			image {
				width: 100%;
				height: 100%;
			}

			view {
				position: absolute;
				top: -4rpx;
				right: -4rpx;
				width: 32rpx;
				height: 32rpx;
				background: #a9070c;
				border-radius: 16rpx;
				font-size: 16rpx;
				color: #fff;
			}
		}

		text {
			font-size: 24rpx;

			text {
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #111111;
			}
		}
	}
}

.jztit {
	display: flex;
	align-items: center;
	justify-content: space-between;
	box-sizing: border-box;
	padding: 50rpx;
	height: 100rpx;

	.titl {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		text {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #111111;
		}

		view {
			width: 60rpx;
			height: 8rpx;
			background: #c7a46f;
		}
	}

	.loc {
		font-family: PingFang-SC, PingFang-SC;
		font-weight: 500;
		font-size: 22rpx;
		color: #c7a46f;
		background: #ede7db;
		border-radius: 20rpx;
		padding: 10rpx 24rpx;
	}
}
</style>
